package GibUI.Sound
{
	import flash.events.TimerEvent;
	import DramScript.IScriptable;
    import mx.containers.Canvas;
    import flash.media.Sound; 
   	import flash.media.SoundChannel; 
    import mx.core.SoundAsset;
 	import flash.media.SoundTransform;
 	import mx.core.Application;
 	import flash.events.Event;
	import utils.Trace;
 	import flash.net.SharedObject;
 	import DramScript.ExecuteCommandResult;
 	
	public class SoundControlAS extends Canvas  implements IScriptable
	{
        protected var soundFXChannel:SoundChannel;
        protected var musicChannel:SoundChannel;
        private var _musicName:String;
        private var _soundFXName:String;
		private var mySO:SharedObject;
		
		public function SoundControlAS(){
			super();
			soundFXChannel = new SoundChannel;
			musicChannel = new SoundChannel;
			mySO = SharedObject.getLocal( "ChipWits_Sound" );
//			mySO.clear(); // Get rid of all data
			// if mySO.size == 0 then this is the first time the game
			// has run on this machine
			if( mySO.size == 0){
				this.soundFXVolume = 1.0;
				this.musicVolume = 1.0;
			}
		}
		public function tickTock(event:TimerEvent):void
		{
		}
		public function executeCommand(target:String, command:String, paramArr:Array):ExecuteCommandResult
		{
			var ecr:ExecuteCommandResult = new ExecuteCommandResult();
			Trace.execCommTrace("soundcontrolas", target, command, paramArr );
			switch( command ){
				case "soundfx":
					this.playSound( paramArr[0]);
				break;
				case "music":
					this.playMusic( paramArr[0]);
					this._musicName = paramArr[0]; // Store this for unmuting
				break;
				case "musicvolume":
					this.setMusicVolume( paramArr[0] );
				break;
				case "soundfxvolume":
					this.setSoundFXVolume( paramArr[0] );
				break;
				case "hidecontrol":
					Application.application.setChildIndex( this, 0 );
				break;
				case "showcontrol":
					Application.application.setChildIndex( this, Application.application.numChildren -1 );
				break;
				default:
					Trace.throwError("can't handle command: "+command);
				break;
			}
			return ecr;
		}
       public function playSound(soundName:String):void {
        	if( ! isMuted) {
        		this._soundFXName = soundName;
             	soundFXChannel=this[soundName + "Snd"].play();
             	setSoundFXVolume( this.soundFXVolume );
          	}
         }    
       public function playMusic(musicName:String):void {
        	if( ! isMuted) {
        		musicChannel.stop();
             	musicChannel=this[musicName + "Snd"].play();
             	setMusicVolume( this.musicVolume );
             	this._musicName = musicName;
             	// Repeat when music ends
             	this.musicChannel.addEventListener( flash.events.Event.SOUND_COMPLETE, repeatMusic,false,0,true );
          	}
         }    
		public function set isMuted( mute:Boolean  ):void {
			this.mySO.data.isMuted = mute;
			this.mySO.flush();
		}
  		public function get isMuted():Boolean {
  			return this.mySO.data.isMuted; 
  		}
		public function set soundFXVolume( vol:Number ):void {
			this.mySO.data.soundFXVolume = vol;
			this.mySO.flush();
		}
  		public function get soundFXVolume():Number {
  			return this.mySO.data.soundFXVolume; 
  		}
		public function set musicVolume( vol:Number ):void {
			this.mySO.data.musicVolume = vol;
			this.mySO.flush();
		}
  		public function get musicVolume():Number {
  			return this.mySO.data.musicVolume; 
  		}
       private function repeatMusic(e:Event):void {
        	this.playMusic( this._musicName );
        }
        protected function setSoundFXVolume( vol:Number ):void {
             if( soundFXChannel != null ){
             	this.soundFXVolume = vol;
	            var transform:SoundTransform = soundFXChannel.soundTransform;
	            transform.volume = vol;
	            soundFXChannel.soundTransform = transform;
            }
        }
         protected function setMusicVolume( vol:Number ):void {
             if( soundFXChannel != null ){
             	this.musicVolume = vol;
	            var transform:SoundTransform = musicChannel.soundTransform;
	            transform.volume = vol ;
	            musicChannel.soundTransform = transform;
            }
        }
      public function stopSound():void {
         	if( soundFXChannel != null){
             	soundFXChannel.stop();
          	}
         	if( musicChannel != null){
             	musicChannel.stop();
          	}
         }    
         public function muteOnOff():void {
         	this.isMuted = !this.isMuted;
         	if( this.isMuted ){
         		stopSound();
         	} else {
 				playMusic( this._musicName );
         	}
         }
    }
}